package myApp.KnorrAndroid.ultils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import myApp.Constant.Constants;
import myApp.KnorrAndroid.view.KnorrBaseActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.InputFilter;
import android.widget.EditText;

public class KnorrUltils {
	
	public KnorrUltils(){
		
	}
	
	/**
	 *  Chup anh
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Dec 28, 2011
	 */
	public static File takePhoto(KnorrBaseActivity activity, int requestCode) {
		final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		final File path = new File(Environment.getExternalStorageDirectory(),
				activity.getApplicationContext().getPackageName());
		if (!path.exists()) {
			path.mkdir();
		}
		String fileName = Constants.TEMP_IMG;
		File retFile = new File(path, fileName);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(retFile));
		activity.startActivityForResult(intent, requestCode);
		return retFile;
	}
	
	/**
	 *  save doi tuong xuong file
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Dec 28, 2011
	 */
	public static void saveObject(Context ct, Object object, String fileName) {
		try {
			FileOutputStream fos = ct.openFileOutput(fileName,
					Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(object);
			oos.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 *  doc doi tuong tu file
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Dec 28, 2011
	 */
	public static Object readObject(Context ct, String fileName) {
		Object object = null;
		try {
			FileInputStream fis = ct.openFileInput(fileName);
			ObjectInputStream ois = new ObjectInputStream(fis);
			object = ois.readObject();
			ois.close();
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
	
	/**
	 *  doi don vi dpi <-> pixel
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Dec 28, 2011
	 */
	public static int dip2Pixel(Context context, int dips) {
		int ret = (int) (context.getResources().getDisplayMetrics().density
				* dips + 0.5f);
		return ret;
	}
	
	/**
	 *  set max lengh cho textview
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Dec 28, 2011
	 */
	public static void setEditTextMaxLength(EditText edText, int maxLength) {
		InputFilter[] inputFilters = new InputFilter[1];
		inputFilters[0] = new InputFilter.LengthFilter(maxLength);
		edText.setFilters(inputFilters);
	}
}
